package com.infocollect.project.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.infocollect.common.utils.ServletUtils;
import com.infocollect.framework.security.LoginUser;
import com.infocollect.framework.security.service.TokenService;
import com.infocollect.project.api.entity.TappMenuActionEntity;
import com.infocollect.project.api.helper.CurrentHelp;
import com.infocollect.project.api.mapper.TappMenuActionMapper;
import com.infocollect.project.api.req.TappMenuActionReq;
import com.infocollect.project.api.service.ITappMenuActionService;
import com.infocollect.project.form.vo.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 菜单动作信息保存功能
 */
@Service
public class TappMenuActionServiceImpl extends ServiceImpl<TappMenuActionMapper, TappMenuActionEntity> implements ITappMenuActionService {
    @Autowired
    private TokenService tokenService;
    /**
     * 保存菜单动作
     */
    @Override
    public R<String> addAction(TappMenuActionReq req) {
        LambdaQueryWrapper<TappMenuActionEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TappMenuActionEntity::getMenuId, req.getMenuId()).eq(TappMenuActionEntity::getActionCode, req.getActionCode());
        if (baseMapper.selectCount(queryWrapper) > 0) {
            return R.newError("动作编码已经存在！");
        }
        LoginUser currentUserInfo = tokenService.getLoginUser(ServletUtils.getRequest());
        TappMenuActionEntity entity = new TappMenuActionEntity();
        BeanUtils.copyProperties(req, entity);
        entity.setFcreateby(currentUserInfo.getUserName());
        entity.setFcreationdate(new Date());
        entity.setFlastupdateby(currentUserInfo.getUserName());
        entity.setFlastupdatedate(new Date());
        baseMapper.insert(entity);
        return R.newOk();
    }

    /**
     * 保存菜单动作
     */
    @Override
    public R<String> eidtAction(TappMenuActionReq req) {
        TappMenuActionEntity entity = baseMapper.selectById(req.getId());
        entity.setActionName(req.getActionName());
        LoginUser currentUserInfo = tokenService.getLoginUser(ServletUtils.getRequest());
        entity.setFlastupdateby(currentUserInfo.getUserName());
        entity.setFlastupdatedate(new Date());
        baseMapper.updateById(entity);
        return R.newOk();
    }

    /**
     * 根据ID 获取按钮信息
     */
    @Override
    public TappMenuActionEntity getActionInfo(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 根据菜单ID 获取对应按钮权限
     */
    @Override
    public List<TappMenuActionEntity> getActionByMenuId(Long menuId) {
        LambdaQueryWrapper<TappMenuActionEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TappMenuActionEntity::getMenuId, menuId);
        List<TappMenuActionEntity> actionlist = baseMapper.selectList(queryWrapper);

        return actionlist;
    }

    /**
     * 根据菜单ID和动作编码获取菜单动作信息
     * @param menuId
     * @param actionCode
     * @return
     */
    @Override
    public  TappMenuActionEntity getActionByMenuIdActionCode(Long menuId,String actionCode){
        LambdaQueryWrapper<TappMenuActionEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TappMenuActionEntity::getMenuId, menuId).eq(TappMenuActionEntity::getActionCode,actionCode);
     return   baseMapper.selectOne(queryWrapper);
    }
    /**
     * 删除动信息
     *
     * @param id
     * @return
     */
    @Override
    public R<String> delAction(Long id) {
        baseMapper.deleteById(id);
        return R.newOk();
    }
}
